package com.itheima;

import java.util.*;
import java.util.stream.Collectors;

public class Topic15 {


    public static List<List<Integer>> threeSum(int[] nums) {

        Arrays.sort(nums);
        Set<List<Integer>> res = new HashSet<List<Integer>>();

        int a = 1; //左指针
        int b = nums.length - 1; //右指针

        for(int i = 0; i < nums.length; i++) {
            a = i + 1;
            b = nums.length - 1;
            while(a < b) {
                int sum = nums[a] + nums[b];
                List<Integer> list = new ArrayList<>();
                if (sum + nums[i] == 0) {
                    list.add(nums[i]);
                    list.add(nums[a]);
                    list.add(nums[b]);
                    res.add(list);
                }
                if(sum + nums[i] > 0) {
                    b--;
                } else {
                    a++;
                }
            }

        }

        System.out.println(res.stream().collect(Collectors.toList()));
        return res.stream().collect(Collectors.toList());
    }

    public static void main(String[] args) {
        Topic15.threeSum(new int[]{-1,0,1,2,-1,-4,-2,-3,3,0,4});
    }
}
